<?php
$access_level = 1;
$script ='survey';
require_once '../../controller.php';
require_once '../../classes/Survey.php';
require_once '../../classes/Question.php'; 

if(isset($_SESSION['logged_in'])){
	$instructor_id = $_SESSION['logged_in'];
} 

if(isset($_REQUEST['sid'])){
 $survey_id = $_REQUEST['sid'];
}

if(isset($_POST['set_condition'])){
	
	$question1_id = $_POST['question_1'];
	$question2_id = $_POST['question_2'];
	$condition = $_POST['condition'];
	$condition_value = $_POST['condition_value'];
	
	$sql = "INSERT INTO `branching` (`survey_id`,`question_1_id`,`question_2_id`,`condition`,`value`)
	                     VALUES ('$survey_id','$question1_id','$question2_id','$condition','$condition_value')";
	$result = mysql_query($sql) or die("Insert Condition:".mysql_error());
	
	if($result){
		header("location:branching.php?sid=$survey_id");
	}
	
}
require_once '../../header.php';

echo "<br><br>";
echo "<h1 align='center'>Set Conditions Here</h1>";
$s = new Survey();
$survey = $s->getSurvey($survey_id);
$survey_name= $survey['name'];
echo "<h1 align='center'><u>Survey Name:</u> ".$survey_name."</h1><br>";
echo "<table align='right'>";
echo "<tr><td width='200px'><a href='question.php?sid=$survey_id'><input type='button' Value='Go Back To Questions'></a></td>";
//echo "<td width='200px'><a href='preview.php?sid=$survey_id'><input type='button' Value='Preview Survey'></a></td>";
//echo "<td width='200px'><a href='finish.php?sid=$survey_id'><input type='button' Value='Finish Survey'></a></td><tr>";
echo "</table><br>";
?>

<?php
$s = new Survey();
$questions = $s->getQuestions($survey_id);
if($questions){
	$sec = 0;
	foreach($questions as $i=>$value){
		if($i !=0 && $value->create_section =='Y'){
			$sec++;
		}
		$question_data['id'] = $value->id; 
		$question_data['question']= $value->question;
		$question_data['type']=$value->type;
		$section[$sec][$i]= $question_data;
	}
	
	echo "<br><br>";
	echo "<b>Display Question This </b><br>";
	echo "<form name='condition_form' method='post'>";
	echo "<select name='question_1' size='3' >";
	foreach($section as $i=>$values){
	foreach($values as $key=>$value){	
	if($i>0 && $value['type'] =='Section Text'){
	echo "<option onclick='javascript: showSection($i);' value='".$value['id']."'>SECTION:->".$value['question']."</option>";	
	}	
	}
}
	echo "</select><br><br>";
	
	echo "<b>IF Answer to Question This</b> <br>";
	
	echo "<select name='question_2' size='3' >";
	foreach($questions as $i=>$value){
		$id = $value->id;
		$type = $value->type;
		$required = $value->required;
		$question = $value->question;
		$create_section = $value->create_section;
	
	if($type !='Section Text'){
	echo "<option value='$id'>$question</option>";	
	}	
	}
	echo "</select><br><br>";
	echo "<b>IS</b> <br>";
	echo "<select name='condition' size='3' >";
	echo "<option value='equal' >Equal To</option><option value='less' >Less Then</option><option value='greater' >Greater Then</option>";
	echo "</select>";	
	echo "&nbsp;&nbsp;&nbsp;"	;
	echo "<input type='text' name='condition_value'><br><br>";
	echo "<input type='submit' name='set_condition' value='Set Condition'> ";
	echo "</form><br><br>";
?>
<div id='condition-info-display'>
<p><b>Setting Conditions:</b> Condition setting for questions act as a skip logic. The question is displayed
to viwer/respondents if answer to particular previous questions meet some criteria as set by conditions.</p><br></br>
<p><b>No of Conditions</b>You can set as many conditions as desired or required by survey. If any questions have more than one 
condition then all the conditions are linked with Logical "OR" relation  i.e. if any of the condition is fullfilled question is displayed.</p><br></br>

</div>
<?php 	
}else{
	echo "<h1 align='center'>Please Add few questions before setting conditions<h1>";
}

$s = new Survey();
$q = new Question();
$conditions = $s->getAllConditions($survey_id);
if($conditions){
foreach($conditions as $c=>$values){
	echo  "<font color='black' size ='5em'>Condition number. ".($c+1)."</font><br>";
	echo  "<font color='black' size ='5em'>Display Question:  </font>";
	foreach($values as $key=>$value){
	if($key == 'question_1_id'){
		$question_1_id = $value;	
	    }
	if($key == 'question_2_id'){
		$question_2_id = $value;	
	    }	
	if($key == 'condition'){
		$condition = $value;	
	    }
	if($key == 'condition_value'){
		$condition_value = $value;	
	    }
	}
	$question1 = $q->getQuestion($question_1_id);
	echo $question1->question."<br>";
	echo "<font color='black' size ='5em'>IF Answer to Question: </font>";
	$question2 = $q->getQuestion($question_2_id);
	echo $question2->question."<br>";
	echo "IS ";
	if($condition =='equal'){
		echo "<font color='black' size ='5em'>Equal To</font>";
	}
    if($condition =='less'){
		echo "<font color='black' size ='5em'>Less Then</font>";
	}
    if($condition =='greater'){
		echo "<font color='black' size ='5em'>Greater Then</font>";
	}
	
	echo "&nbsp;&nbsp;&nbsp;&nbsp;";
	echo $condition_value;
	echo "<br>--------------------------------------------------------------------------<br><br>";
	
}
}else{
	echo "<h1 align='center'>You haven't set any condition yet. <h1>";
}

require_once '../../footer.php';
?>